package com.blast.cn.ui

import android.view.KeyEvent
import com.blast.cn.R
import com.blast.cn.persenter.check.CheckDeviceSerialPortPresenter
import com.blast.cn.persenter.check.CheckDeviceStoragePresenter
import com.blast.cn.persenter.check.CheckDeviceTimePresenter
import com.wxl.common.base.AbsActivity
import com.wxl.common.base.Works
import com.wxl.common.util.ImgLoader
import kotlinx.android.synthetic.main.activity_check_device_layout.*

class CheckDeviceActivity : AbsActivity() {

    private  var mWorks: Works?=null

    override fun getCreateContentViewId(): Int {
        return R.layout.activity_check_device_layout
    }

    override fun onCreateViewChanged() {
        setTopTitle("设备自检")
        ImgLoader.loadResouce(checkCompanyImageView, R.mipmap.icon_company)
        startCheckDevice()
        retryBtn.setOnClickListener {
            startCheckDevice()
        }
    }

    override fun onKeyDownEvent(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true
        }
        if(keyCode == KeyEvent.KEYCODE_F4 ){
            if(retryBtn.isShown){
                retryBtn.performClick()
            }
        }
        return true
    }

    /**
     * 开始自检
     */
    private fun startCheckDevice() {
        mWorks = Works.Builder(this)
            .add(CheckDeviceTimePresenter::class.java, checkDeviceLayout)
            .add(CheckDeviceStoragePresenter::class.java, checkDeviceLayout)
            .add(CheckDeviceSerialPortPresenter::class.java, checkDeviceLayout)
            .queue()
    }

    override fun onDestroy() {
        super.onDestroy()
        mWorks?.releaseWorks()
    }
}